/*
 * This file is part of aion-lightning <aion-lightning.org>.
 * 
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.actions;

import com.aionengine.gameserver.controllers.observer.ActionObserver;
import com.aionengine.gameserver.model.EmotionType;
import com.aionengine.gameserver.model.gameobjects.player.InRoll;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.gameobjects.state.CreatureState;
import com.aionengine.gameserver.model.templates.ride.RideInfo;
import com.aionengine.gameserver.model.templates.windstreams.WindstreamPath;
import com.aionengine.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author xTz
 */
public class PlayerActions extends CreatureActions {

    public static boolean isInPlayerMode(Player player, PlayerMode mode) {
        switch (mode) {
            case RIDE:
                return player.ride != null;
            case IN_ROLL:
                return player.inRoll != null;
            case WINDSTREAM:
                return player.windstreamPath != null;
        }
        return false;
    }

    public static void setPlayerMode(Player player, PlayerMode mode, Object obj) {
        switch (mode) {
            case RIDE:
                player.ride = (RideInfo) obj;
                break;
            case IN_ROLL:
                player.inRoll = (InRoll) obj;
                break;
            case WINDSTREAM:
                player.windstreamPath = (WindstreamPath) obj;
                break;
        }
    }

    public static boolean unsetPlayerMode(Player player, PlayerMode mode) {
        switch (mode) {
            case RIDE:
                RideInfo ride = player.ride;
                if (ride == null) {
                    return false;
                }
                //check for sprinting when forcefully dismounting player
                if (player.isInSprintMode()) {
                    player.getLifeStats().triggerFpRestore();
                    player.setSprintMode(false);
                }
                player.unsetState(CreatureState.RESTING);
                player.unsetState(CreatureState.FLOATING_CORPSE);
                player.setState(CreatureState.ACTIVE);
                player.ride = null;
                PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.START_EMOTE2, 0, 0), true);
                PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.RIDE_END), true);

                player.getGameStats().updateStatsAndSpeedVisually();

                //remove rideObservers
                for (ActionObserver observer : player.getRideObservers()) {
                    player.getObserveController().removeObserver(observer);
                }
                player.getRideObservers().clear();
                return true;
            case IN_ROLL:
                if (player.inRoll == null) {
                    return false;
                }
                player.inRoll = null;
                return true;
            case WINDSTREAM:
                if (player.windstreamPath == null) {
                    return false;
                }
                player.windstreamPath = null;
                return true;
            default:
                return false;
        }
    }

}